package cn.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.MenuEntity;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.RoleEntity;
import cn.kpi.entity.sys.RoleMenuEntity;
import cn.kpi.entity.sys.RoleOrgEntity;
import cn.kpi.entity.sys.RoleUserEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.service.sys.IOrgService;
import cn.kpi.service.sys.IRightsService;
import cn.kpi.service.sys.IUserService;

@Controller
@RequestMapping("rights")
public class RightsController {
	@Autowired
	private IRightsService iRightsService;
	
	@Autowired
	private IUserService iUserService;
	
	@Autowired 
	private IOrgService iOrgService;
	 
	
	@RequestMapping(value = "/rights_list")
	public String RightsList(Model model) {
		try
		{
			
		}
		catch(Exception ex){
			
		}
		return "sys/rights_list";
	}
	
	@RequestMapping(value = "/rights_user")
	public String RightsUser(Model model,Long roleid) {
		try
		{
			List<UserEntity> lstuser = iUserService.GetUserListByWhere("");
			model.addAttribute("lstuser", lstuser);

			List<OrgEntity> orglst = iOrgService.GetOrgList();
			for (int i = 0; i < orglst.size(); i++) {
				String html = "";
				for (int j = 0; j < orglst.get(i).getLevel(); j++) {
					html += "&#12288;";
				}
				orglst.get(i).setOrgname(html + orglst.get(i).getOrgname());
			}
			model.addAttribute("orglst", orglst);
			model.addAttribute("roleid", roleid);
		}
		catch(Exception ex){
			
		}
		return "sys/rights_user";
	}
	
	@RequestMapping(value = "/rights_menu")
	public String RightsMenu(Model model,Long roleid) {
		try
		{
			model.addAttribute("roleid", roleid);
		}
		catch(Exception ex){
			
		}
		return "sys/rights_menu";
	}
	
	@RequestMapping(value = "/rights_org")
	public String RightsOrg(Model model,Long roleid) {
		try
		{
			model.addAttribute("roleid", roleid);
		}
		catch(Exception ex){
			
		}
		return "sys/rights_org";
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<RoleEntity> GetList(HttpServletRequest request) {

		GridCommon<RoleEntity> countlist = new GridCommon<RoleEntity>();
		List<RoleEntity> lst=null;
		LayTable<RoleEntity> layTable=new LayTable<RoleEntity>();
		try {
			String RoleName = request.getParameter("rolename");
			String where = "1=1 ";
			if (RoleName != null && RoleName.length() > 0) {
				where += " AND rolename like '%" + RoleName + "%'";
			}

			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;
			
			lst = iRightsService.GetRoleListLimit(where, beginrow, endrow);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}
			
			layTable.setCode(0);
			layTable.setCount(iRightsService.GetRoleListLimitCount(where));
			layTable.setData(lst);
			
		} catch (Exception e) {
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(null);
			countlist.setStatus(1);
			countlist.setMsg("操作成功");
		}
		return layTable;
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_user_list")
	public LayTable<RoleUserEntity> GetUserList(HttpServletRequest request) {

		List<RoleUserEntity> lst = null;
		LayTable<RoleUserEntity> layTable = new LayTable<RoleUserEntity>();
		try {
			String realname = request.getParameter("realname");
			String szdw = request.getParameter("szdw");
			String strroleid=request.getParameter("roleid");
			String where = "";
			if (realname != null && realname.length() > 0) {
				where += " AND realname LIKE '%" + realname + "%'";
			}
			if (szdw != null && szdw.length() > 0 && !szdw.equals("0")) {

				OrgEntity orgentity = iOrgService.GetOrgById(Long.parseLong(szdw));

				where += " AND szdw IN (SELECT orgid FROM t_sys_org WHERE lft>=" + orgentity.getLft() + " AND lft<="
						+ orgentity.getRgt() + ")";
			}

			lst = iRightsService.GetRoleUserList(where, Long.parseLong(strroleid));
			for(int i=0;i<lst.size();i++){
				if(lst.get(i).getIschecked()==1){
					lst.get(i).setLay_CHECKED(true);
				}else{
					lst.get(i).setLay_CHECKED(false);
				}
				
			}

			int iCount = 0;
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_menu_list")
	public LayTable<RoleMenuEntity> GetMenuList(HttpServletRequest request) {
		List<RoleMenuEntity> lst=null;
		LayTable<RoleMenuEntity> layTable=new LayTable<RoleMenuEntity>();
		try {
			Long roleid = Long.parseLong(request.getParameter("roleid"));
			
			lst = iRightsService.GetRoleMenuList(roleid);
			for(int i=0;i<lst.size();i++){
				if(lst.get(i).getIschecked()==1){
					lst.get(i).setLay_CHECKED(true);
				}else{
					lst.get(i).setLay_CHECKED(false);
				}
			}

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_org_list")
	public LayTable<RoleOrgEntity> GetOrgList(HttpServletRequest request) {
		List<RoleOrgEntity> lst=null;
		LayTable<RoleOrgEntity> layTable=new LayTable<RoleOrgEntity>();
		try {
			Long roleid = Long.parseLong(request.getParameter("roleid"));
			
			lst = iRightsService.GetRoleOrgList(roleid);
			for(int i=0;i<lst.size();i++){
				if(lst.get(i).getIschecked()==1){
					lst.get(i).setLay_CHECKED(true);
				}else{
					lst.get(i).setLay_CHECKED(false);
				}
			}

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			e.printStackTrace();
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/insert_roleuser")
	public ExceResult InsertRoleUser(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long roleid=Long.parseLong(request.getParameter("roleid"));
		 String struserids=request.getParameter("userids");
		 String[] useridArr=struserids.split(",");
		 
		 List<Long> list=new ArrayList<Long>();
		 for(int i=0;i<useridArr.length;i++){
			 list.add(Long.parseLong(useridArr[i]));
		 }
		 
		 int iResult=iRightsService.InsertRoleUser(list,roleid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/insert_rolemenu")
	public ExceResult InsertRoleMenu(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long roleid=Long.parseLong(request.getParameter("roleid"));
		 String strmenuids=request.getParameter("menuids");
		 String[] menuidArr=strmenuids.split(",");
		 
		 List<Long> list=new ArrayList<Long>();
		 for(int i=0;i<menuidArr.length;i++){
			 list.add(Long.parseLong(menuidArr[i]));
		 }
		 
		 int iResult=iRightsService.InsertRoleMenu(list,roleid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/insert_roleorg")
	public ExceResult InsertRoleOrg(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long roleid=Long.parseLong(request.getParameter("roleid"));
		 String strorgids=request.getParameter("orgids");
		 String[] orgidArr=strorgids.split(",");
		 
		 List<Long> list=new ArrayList<Long>();
		 for(int i=0;i<orgidArr.length;i++){
			 list.add(Long.parseLong(orgidArr[i]));
		 }
		 
		 int iResult=iRightsService.InsertRoleOrg(list,roleid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
}
